﻿@model ProductModel.AddRequiredProductModel
@inject AdminAreaSettings adminAreaSettings

@{
    Layout = "_AdminPopupLayout";

    var popupGridPageSize = adminAreaSettings.PopupGridPageSize;

    //page title
    ViewBag.Title = T("Admin.Catalog.Products.Fields.RequiredProductIds.Choose").Text;
}


<nop-antiforgery-token />
<div class="content-header clearfix">
    <h1 class="pull-left">
        @T("Admin.Catalog.Products.Fields.RequiredProductIds.Choose")
    </h1>
    <div class="pull-right">
        &nbsp;
    </div>
</div>

<div class="content">
    <div class="form-horizontal">
        <div class="panel-group">
            <div class="panel panel-default panel-search panel-popup">
                <div class="panel-body">
                    <div class="row">
                        <div class="col-sm-6">
                            <div class="form-group">
                                <div class="col-sm-5">
                                    <nop-label asp-for="SearchProductName" />
                                </div>
                                <div class="col-sm-7">
                                    <nop-editor asp-for="SearchProductName" />
                                </div>
                            </div>
                            <div class="form-group" @(Model.AvailableCategories.SelectionIsNotPossible() ? Html.Raw("style=\"display:none\"") : null)>
                                <div class="col-sm-5">
                                    <nop-label asp-for="SearchCategoryId" />
                                </div>
                                <div class="col-sm-7">
                                    <nop-select asp-for="SearchCategoryId" asp-items="Model.AvailableCategories" />
                                </div>
                            </div>
                            <div class="form-group" @(Model.AvailableVendors.SelectionIsNotPossible() || Model.IsLoggedInAsVendor ? Html.Raw("style='display: none;'") : null)>
                                <div class="col-sm-5">
                                    <nop-label asp-for="SearchVendorId" />
                                </div>
                                <div class="col-sm-7">
                                    <nop-select asp-for="SearchVendorId" asp-items="Model.AvailableVendors" />
                                </div>
                            </div>
                        </div>
                        <div class="col-sm-6">
                            <div class="form-group" @(Model.AvailableStores.SelectionIsNotPossible() ? Html.Raw("style=\"display:none\"") : null)>
                                <div class="col-sm-5">
                                    <nop-label asp-for="SearchStoreId" />
                                </div>
                                <div class="col-sm-7">
                                    <nop-select asp-for="SearchStoreId" asp-items="Model.AvailableStores" />
                                </div>
                            </div>
                            <div class="form-group">
                                <div class="col-sm-5">
                                    <nop-label asp-for="SearchProductTypeId" />
                                </div>
                                <div class="col-sm-7">
                                    <nop-select asp-for="SearchProductTypeId" asp-items="Model.AvailableProductTypes" />
                                </div>
                            </div>
                            <div class="form-group" @(Model.AvailableManufacturers.SelectionIsNotPossible() ? Html.Raw("style=\"display:none\"") : null)>
                                <div class="col-sm-5">
                                    <nop-label asp-for="SearchManufacturerId" />
                                </div>
                                <div class="col-sm-7">
                                    <nop-select asp-for="SearchManufacturerId" asp-items="Model.AvailableManufacturers" />
                                </div>
                            </div>
                        </div>
                    </div>
                    <div class="row">
                        <div class="col-sm-12">
                            <button type="button" id="search-products" class="btn btn-primary btn-search">
                                <i class="fa fa-search"></i>
                                @T("Admin.Common.Search")
                            </button>
                        </div>
                    </div>
                </div>
            </div>
        </div>

        <div class="panel panel-default">
            <div class="panel-body">
                <div id="products-grid"></div>

                <script type="text/javascript">
                        $(document).ready(function () {
                            $("#products-grid").kendoGrid({
                                dataSource: {
                                    type: "json",
                                    transport: {
                                        read: {
                                            url: "@Html.Raw(Url.Action("RequiredProductAddPopupList", "Product"))",
                                            type: "POST",
                                            dataType: "json",
                                            data: additionalData
                                        }
                                    },
                                    schema: {
                                        data: "Data",
                                        total: "Total",
                                        errors: "Errors"
                                    },
                                    error: function(e) {
                                        display_kendoui_grid_error(e);
                                        // Cancel the changes
                                        this.cancelChanges();
                                    },
                                    pageSize: @(popupGridPageSize),
                                    serverPaging: true,
                                    serverFiltering: true,
                                    serverSorting: true
                                },
                                pageable: {
                                    refresh: true,
                                    @await Html.PartialAsync("_GridPagerMessages")
                                },
                                editable: {
                                    confirmation: "@T("Admin.Common.DeleteConfirmation")",
                                    mode: "inline"
                                },
                                scrollable: false,
                                columns: [{
                                    field: "Name",
                                    title: "@T("Admin.Common.Select")",
                                    width: 50,
                                    template: '<button type="button" onclick="selectRequiredProduct(#=Id#)" class="btn btn-default">@T("Admin.Common.Select")</button>'
                                },{
                                    field: "Name",
                                    title: "@T("Admin.Catalog.Products.Fields.Name")"
                                }, {
                                    field: "Published",
                                    title: "@T("Admin.Catalog.Products.Fields.Published")",
                                    width: 100,
                                    headerAttributes: { style: "text-align:center" },
                                    attributes: { style: "text-align:center" },
                                    template: '# if(Published) {# <i class="fa fa-check true-icon"></i> #} else {# <i class="fa fa-close false-icon"></i> #} #'
                                }]
                            });
                        });
                </script>
            </div>
        </div>
    </div>

    <script type="text/javascript">
            $(document).ready(function () {
                $('#search-products').click(function () {
                    var grid = $('#products-grid').data('kendoGrid');
                    grid.dataSource.page(1); //new search. Set page size to 1
                    //grid.dataSource.read(); we already loaded the grid above using "page" function
                    return false;
                });
            });

            $("#@Html.IdFor(model => model.SearchProductName)").keydown(function (event) {
                if (event.keyCode == 13) {
                    $("#search-products").click();
                    return false;
                }
            });

            function additionalData() {
                var data = {
                    SearchProductName: $('#@Html.IdFor(model => model.SearchProductName)').val(),
                    SearchCategoryId: $('#SearchCategoryId').val(),
                    SearchManufacturerId: $('#SearchManufacturerId').val(),
                    SearchStoreId: $('#SearchStoreId').val(),
                    SearchVendorId: $('#SearchVendorId').val(),
                    SearchProductTypeId: $('#SearchProductTypeId').val()
                };
                addAntiForgeryToken(data);
                return data;
            }

            function selectRequiredProduct(productid) {
                try {
                    var currentValue = window.opener.document.getElementById('@(Context.Request.Query["productIdsInput"])').value;
                    if (currentValue) {
                        //add comma
                        window.opener.document.getElementById('@(Context.Request.Query["productIdsInput"])').value += ', ';
                    }
                    window.opener.document.getElementById('@(Context.Request.Query["productIdsInput"])').value += productid;
                    window.opener.document.getElementById('@(Context.Request.Query["btnId"])').click();
                }
                catch (e){}
                window.close();
            }
    </script>
</div>